﻿namespace Nomo.CoreModule
{
    /// <summary>
    /// 队列池。
    /// </summary>
    /// <typeparam name="T">队列的成员的类型。</typeparam>
    public static class QueuePool<T>
    {
        private static readonly ObjectPool<System.Collections.Generic.Queue<T>> Pool = new ObjectPool<System.Collections.Generic.Queue<T>>(() => new System.Collections.Generic.Queue<T>(), default, e => e.Clear());

        /// <summary>
        /// 从队列池里取出一个空队列。
        /// </summary>
        /// <returns>一个空队列。</returns>
        public static System.Collections.Generic.Queue<T> Get()
        {
            return Pool.Get();
        }

        /// <summary>
        /// 将一个队列放回队列池。
        /// </summary>
        /// <param name="queue">要放回队列池的队列。</param>
        public static void Release(System.Collections.Generic.Queue<T> queue)
        {
            if (queue != null)
            {
                Pool.Release(queue);
            }
        }

        /// <summary>
        /// 移除队列池中的所有队列。
        /// </summary>
        public static void Clear()
        {
            Pool.Clear();
        }
    }
}
